﻿package Server.AHMJ.LAMJ;

import java.util.HashMap;
import java.util.Map;

import Server.Agent.GameAgent;
import Server.Model.Base.IRoom;
import Server.Model.Base.SHMUser;
import Server.Model.Hall.QpGameMgr;
import Server.Model.Hall.dthall;
import common.routine.GlobalSetting;
import net.sf.json.JSONObject;

public class MajTableMgr {
	static MajTableMgr __instance = new MajTableMgr();

	public static MajTableMgr instance() {
		return __instance;
	}

	Long m_LastDealTime;
	Long m_LastUpdateTime;

	static Map<Integer, MajTable> m_mapTable = new HashMap<>();// 房间号 . 房间
	Map<Integer, Long> m_mapHeartTime = new HashMap<>();// 心跳时间

	void Init() {
		m_LastDealTime = 0L;
		m_LastUpdateTime = 0L;
	}

	static MajTable FindTable(int _Uid) {
		MajTable it = (MajTable) QpGameMgr.getInstance().getUserRoomByUid(_Uid);
		if (it != null) {
			return it;
		}
		return null;
	}

	void Handle_onHeart(int _Uid) {

	}

	public static void Handle_onConnect(GameAgent agent, String[] vecArg) {
		
		SUBCMD_S_Connect cmd = new SUBCMD_S_Connect();
		String sData = cmd.getJsonData();
		agent.SendMessage(sData);
	}

	public static void Handle_onJoin(GameAgent agent, String[] vecArg) {
		SUBCMD_S_Join cmd = new SUBCMD_S_Join();
		String sData = cmd.getJsonData();
		agent.SendMessage(sData);
	}

	public static void Handle_onLeave(GameAgent agent, String[] vecArg) {
		
		Integer _Uid = agent.getUid();
		MajTable pTable = (MajTable)FindTable(_Uid);
		
		if (pTable != null)
		{
			pTable.OnUserStandUp(_Uid,true); 
			
			
			QpGameMgr.getInstance().Handle_OnUserRoom(agent,new JSONObject());
			
		}
			
	}
	
	public static void Handle_onDismissRoom(GameAgent agent, String[] vecArg) {
		Integer _Uid = agent.getUid();
		MajTable tbl = (MajTable)FindTable(_Uid);
//		if(tbl != null)
//		{
//			dthall.getInstance().NotifyRemoveUserRoom(tbl);	
//		}
		if(tbl != null && tbl.m_pIUserItem[0].dwUserID == _Uid && tbl.m_bTableStarted == false)
		{
			QpGameMgr.getInstance().NotifyRemoveUserRoom(tbl);	
			tbl.OnEventGameEnd(0, Constants.GAME_END_DISMISS);
		}
	}
 //QGHBP;PSDBP;HZTB;HZJF;AAPAY;16JU;WUZHUANG
	public static MajTable CreateTable(int _Uid, String strParam, boolean isFangGuan) {
		try {
			int byCostGem = 1;

			SHMUser user = dthall.getInstance().getUserInfo(_Uid);
			if (user == null)
				return null;

			if (strParam == null)
				return null;

			String[] vecParam = strParam.split(";");

			if (vecParam.length == 0)
				return null;

			int jushu = 4;
			String payway = "AAPAY";

			String strRoomDesc = "六安麻将  宝牌";
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];

				if (param.equalsIgnoreCase("4JU")) {
					jushu = 4;
					byCostGem = 1;
					strRoomDesc += " 4局";
				}
				if (param.equalsIgnoreCase("8JU")) {
					jushu = 8;
					byCostGem = 2;
					strRoomDesc += " 8局";
				}
				if (param.equalsIgnoreCase("16JU")) {
					jushu = 16;
					byCostGem = 3;
					strRoomDesc += " 16局";
				}
			}
			
			int hutype = 1;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];

				if (param.equalsIgnoreCase("ZIMO")) {
					hutype = 1;
					strRoomDesc += " 自摸胡 ";
				}
				if (param.equalsIgnoreCase("DIANPAO")) {
					hutype = 0;
					strRoomDesc += " 允许点炮胡 ";					
				}
				
			}
		 
			int hzjz = 0;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];

				if (param.equalsIgnoreCase("HZJZ")) {
					hzjz = 1;
					strRoomDesc += " 红中加嘴 ";
				}
				
			}
			
			int zhuang=0;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];

				if (param.equalsIgnoreCase("WUZHUANG")) {
					zhuang = 0;
					strRoomDesc += " 无庄 ";
				}

				if (param.equalsIgnoreCase("YIFEN")) {
					zhuang = 1;
					strRoomDesc += " 庄分为1 ";
				}

				if (param.equalsIgnoreCase("LIANGFEN")) {
					zhuang = 2;
					strRoomDesc += " 庄分为2 ";
				}
				
			}
			
			int psdbp=0;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];

				if (param.equalsIgnoreCase("PSDBP")) {
					psdbp = 1;
					strRoomDesc += " 碰三对包牌 ";
				}
				
			}
			
			int qghbp=0;

			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];

				if (param.equalsIgnoreCase("QGHBP")) {
					qghbp = 1;
					strRoomDesc += " 抢杠胡包牌 ";
				}
				
			}
			
			
			int duihufb = 1;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];

				if (param.equalsIgnoreCase("QDZFB")) {
					duihufb = 1;
					strRoomDesc += " 对胡翻倍 ";
				}
				if (param.equalsIgnoreCase("QDZBFB")) {
					duihufb = 0;
					strRoomDesc += " 对胡不翻倍 ";					
				}
				
			}
			
			int maPaiCount = 4;
			for (int i = 0; i < vecParam.length; i++)
			{
				String param = vecParam[i];
				
				if (param.equalsIgnoreCase("4MA"))
				{
					maPaiCount = 4;
					strRoomDesc += "4码牌 ";	
				}
				if (param.equalsIgnoreCase( "6MA"))
				{
					maPaiCount = 6;
					strRoomDesc += "6码牌 ";	
				}
				if (param.equalsIgnoreCase( "8MA"))
				{
					maPaiCount = 8;
					strRoomDesc += "8码牌 ";	
				}						
			}
			
			int whzjiama = 1;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];

				if (param.equalsIgnoreCase("JIAMA")) {
					whzjiama = 1;
					strRoomDesc += " 无红中加一码 ";
				}
				if (param.equalsIgnoreCase("BUJIAMA")) {
					whzjiama = 0;
					strRoomDesc += " 无红中不加码 ";					
				}
				
			}
			
			
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];

				if (param.equalsIgnoreCase("AAPAY")) {
					payway = "AAPAY";
				}
				
				if (param.equalsIgnoreCase("FZPAY")) {
					
					payway = "FZPAY";
				}
				
				if(param.equalsIgnoreCase("CLUBPAY")){
					payway = "CLUBPAY";
				}
				
			}						
			
			int maxCost = byCostGem;
			if(payway.equals("AAPAY")){
				if(jushu==16){
					maxCost = 15;
				}				
				if(jushu==8){
				maxCost = 8;
				}
			}
			if(payway.equals("FZPAY") || payway.equals("CLUBPAY")){
				if(jushu==16){
					maxCost = 60;
				}				
				if(jushu==8){
				maxCost = 30;
				}
			}
			int realCost = maxCost;
			if(payway.equals("CLUBPAY")){
				maxCost = 0;
			}
			System.out.println("1[" + maxCost + "]1");
			 
			if (jushu > 0 && byCostGem >= 0 && payway.length() > 0 && user.getGem(0) >= maxCost) {
				int factor = 0;
				for (int i = 0; i < 1000000; i++) {

					int rand_room = GlobalSetting.getInstance().getRandRoomNum();//Constants.rand(100000, 999999, factor);
					
					factor = rand_room;
					MajTable room = (MajTable) QpGameMgr.getInstance().getRoomById(rand_room);
					if (room == null) {
						room = new MajTable();
						room.Init(Constants.INIT_TOTAL);
						room.SetParam(strParam);
						room.SetDesc(strRoomDesc);
						room.m_diFen = 1;
						room.m_fdFen = 0;
						room.m_dhfb = duihufb;
						room.m_hutype = hutype;
						room.m_maPaiCount = maPaiCount;
						room.m_jiama  = whzjiama;
						room.m_dwTableID = rand_room;
						room.m_byCostGem = byCostGem;
						room.m_maxCostGem = realCost;
						room.m_payWay = payway;
						/**
						 * 是否允许红中加嘴
						 */
						room.m_hzjz=hzjz;
						/**
						 * 碰三对包牌
						 */
						room.m_psdbp=psdbp;
						/**
						 * 抢杠胡包牌
						 */
						room.m_qghbp=qghbp;
						/**
						 * 0代表无庄
						 * 1,代表庄分为1
						 * 2，代表庄分为2
						 */
						room.m_zhuang=zhuang;
						room.m_byMaxRound = (int) jushu;
						room.m_Creator = isFangGuan ? _Uid : 0;
						QpGameMgr.getInstance().putRoomIntoMap(rand_room, room);

						return room;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	void DismissTable(MajTable pTable) {

		if (pTable == null)
			return;

		QpGameMgr.getInstance().OnDismissRoom((IRoom) pTable);

		{

			m_mapTable.remove(pTable.GetID());
		}

	}

	void OnDeal(float dt) {
		Long tnow_ms = getCurrentTimeMillis();
		if (m_LastDealTime == 0) {
			m_LastDealTime = tnow_ms;
		}

		if (tnow_ms - m_LastDealTime >= 1000) {
			m_LastDealTime = tnow_ms;

		}
	}

	private Long getCurrentTimeMillis() {
		return System.currentTimeMillis();
	}

	public static void OnZuoPai(int id,int seat,int pai[]){
		Integer lUid = id;
		MajTable tbl = (MajTable)FindTable(lUid);
		if (tbl == null) {
			return;
		}else{
			tbl.OnZuoMj(seat,pai);
		}
	}
	
	public static void OnDealPai(int userId,int seat,int pai){
		Integer lUid = userId;
		MajTable tbl = (MajTable)FindTable(lUid);
		if (tbl == null) {
			return;
		}else{
			tbl.OnMJMsg(seat,pai);
		}
	}
	
	public static void OnRecv(GameAgent agent, JSONObject root) {
		String data = root.optString("data");
		String vecArg2[] = data.split("\\|");
		        if (vecArg2.length == 0){
			         return;
	   	         }
		     	String subcmd = vecArg2[0];
		     	switch (subcmd) {
				case "connect":
					Handle_onConnect(agent,vecArg2);
					return;
				case "join":
					Handle_onJoin(agent,vecArg2);
					return;
				case "leave":
					Handle_onLeave(agent,vecArg2);
					return;
				case "dismissroom":
					Handle_onDismissRoom(agent,vecArg2);
					return;	
//				case "kick":
//					Handle_onKick(agent,vecArg2);
//					return;
			 
				default:
					break;
				}
				Integer lUid = agent.getUid();
				IRoom iterUser = FindTable(lUid);
				if (iterUser == null) {
					return;
				}
				MajTable tbl = (MajTable) (iterUser);
				boolean printLog = true;
					if (subcmd.equals("ready")) {
						tbl.Handle_onReady(lUid, vecArg2);
					} else if (subcmd.equals("robot")) {
						tbl.Handle_onRobot(lUid, vecArg2);
					} else if (subcmd.equals("biaoqing")) {
						tbl.Handle_onBiaoQing(lUid, vecArg2);
					} else if (subcmd.equals("yuyin")) {
						tbl.Handle_onYuYin(lUid, vecArg2);
					} else if (subcmd.equals("chat")) {
						tbl.Handle_onChat(lUid, vecArg2);
					} else if (subcmd.equals("voice")) {
						tbl.Handle_onVoice(lUid, vecArg2);
					} else if (subcmd.equals("voicemsg")) {
						printLog = false;
						tbl.Handle_onVoiceMsg(lUid, vecArg2);
					}else if (subcmd.equals("text")) {
						tbl.Handle_onText(lUid, vecArg2);
					} else if (subcmd.equals("mopai")) {
						tbl.Handle_onMoPai(lUid, vecArg2);
					} else if (subcmd.equals("chupai")) {
						tbl.Handle_onChuPai(lUid, vecArg2);
					} else if (subcmd.equals("chipai")) {
						tbl.Handle_onChiPai(lUid, vecArg2);
					} else if (subcmd.equals("pengpai")) {
						tbl.Handle_onPengPai(lUid, vecArg2);
					} else if (subcmd.equals("gangpai")) {
						tbl.Handle_onGangPai(lUid, vecArg2);
					} else if (subcmd.equals("guopai")) {
						tbl.Handle_onGuoPai(lUid, vecArg2);
					} else if (subcmd.equals("hupai")) {
						tbl.Handle_onHuPai(lUid, vecArg2);
					} else if (subcmd.equals("try_dismissroom")) {
						tbl.Handle_onTryDismissRoom(lUid, vecArg2);
					} else if (subcmd.equals("agree_dismissroom")) {
						tbl.Handle_onAgreeDismissRoom(lUid, vecArg2);
					} else if (subcmd.equals("refuse_dismissroom")) {
						tbl.Handle_onRefuseDismissRoom(lUid, vecArg2);
					}else if (subcmd.equals("inBack")){
						tbl.Handle_InBack(lUid,vecArg2);
					} else if (subcmd.equals("inFore")){
						tbl.Handle_InFore(lUid,vecArg2);
					} else if (subcmd.equals("kick")){
						tbl.Handle_Kick(lUid, vecArg2);
					}
	            }
         }
